<?php

namespace app\forms\user;

use app\forms\Model;
use app\models\User;
use app\models\Group;
use app\models\Position;
use app\models\Department;
use app\models\UserProfile;
use app\components\helpers\MappingHelper;
use app\components\validators\PositiveInteger;

/**
 * 用户表单类
 */
class BaseForm extends Model
{
    public $number;
    public $realName;
    public $username;
    public $password;
    public $mobile;
    public $sex;

    public $groupId;
    public $deptId;
    public $positionId;
    public $roleId;

    public $avatar;
    public $email;
    public $qq;
    public $birthday;
    public $provinceId;
    public $cityId;
    public $districtId;
    public $street;

    public function rules()
    {
        return [
            ['number', 'trim'],
            ['number', 'string', 'length' => [6, 8]],
            ['number', 'unique', 'targetClass' => User::class, 'targetAttribute' => 'number'],

            ['realName', 'trim'],
            ['realName', 'string', 'length' => [2, 4]],

            ['username', 'trim'],
            ['username', 'string', 'length' => [6, 16]],
            ['username', 'unique', 'targetClass' => User::class, 'targetAttribute' => 'username'],

            ['password', 'trim'],
            ['password', 'string', 'length' => [6, 16]],

            ['mobile', 'trim'],
            ['mobile', 'string', 'length' => [11, 11]],
            ['mobile', 'unique', 'targetClass' => UserProfile::class],

            ['sex', 'in', 'range' => [MappingHelper::SEX_MALE, MappingHelper::SEX_FEMALE]],
            ['sex', 'default', 'value' => MappingHelper::SEX_MALE],

            ['groupId', PositiveInteger::class],
            ['groupId', 'exist', 'targetClass' => Group::class, 'targetAttribute' => 'id'],

            ['deptId', PositiveInteger::class],
            ['deptId', 'exist', 'targetClass' => Department::class, 'targetAttribute' => 'id'],

            ['positionId', PositiveInteger::class],
            ['positionId', 'exist', 'targetClass' => Position::class, 'targetAttribute' => 'id'],

            ['roleId', 'each', 'rule' => [PositiveInteger::class]],

            ['avatar', 'trim'],
            ['avatar', 'string', 'max' => 128],
            ['avatar', 'default', 'value' => ''],

            ['email', 'trim'],
            ['email', 'email'],
            ['email', 'default', 'value' => ''],

            ['qq', 'trim'],
            ['qq', 'number'],
            ['qq', 'default', 'value' => ''],

            ['birthday', 'trim'],
            ['birthday', 'string'],
            ['birthday', 'default', 'value' => ''],

            ['provinceId', PositiveInteger::class],
            ['provinceId', 'default', 'value' => 0],

            ['cityId', PositiveInteger::class],
            ['cityId', 'default', 'value' => 0],

            ['districtId', PositiveInteger::class],
            ['districtId', 'default', 'value' => 0],

            ['street', 'trim'],
            ['street', 'string', 'max' => 64],
            ['street', 'default', 'value' => '']
        ];
    }

}